from PySide6.QtWidgets import QDialog, QVBoxLayout, QTextEdit, QPushButton


class ConnectionCheckDialog(QDialog):
    def __init__(self, audio_canvas, parent=None):
        super().__init__(parent)
        self.audio_canvas = audio_canvas
        self.theme = "dark"  # 默认主题
        self.initUI()

    def initUI(self):
        self.setWindowTitle("连接检查")
        self.setModal(True)
        self.resize(500, 400)

        layout = QVBoxLayout()

        # 创建文本显示区域
        self.text_edit = QTextEdit()
        self.text_edit.setReadOnly(True)

        # 收集连接信息
        self.generateReport()

        # 添加关闭按钮
        close_button = QPushButton("关闭")
        close_button.clicked.connect(self.close)

        layout.addWidget(self.text_edit)
        layout.addWidget(close_button)
        self.setLayout(layout)

        # 应用主题
        self.applyTheme(self.theme)

    def setTheme(self, theme):
        """设置对话框主题"""
        self.theme = theme
        self.applyTheme(theme)

    def applyTheme(self, theme):
        """应用主题样式"""
        if theme == "light":
            self.setStyleSheet("""
                QDialog {
                    background-color: #f0f0f0;
                    color: #000000;
                }
                QTextEdit {
                    background-color: #ffffff;
                    color: #000000;
                    border: 1px solid #c0c0c0;
                    border-radius: 3px;
                    font-family: Consolas, 'Courier New', monospace;
                    font-size: 10pt;
                }
                QPushButton {
                    background-color: #e0e0e0;
                    color: #000000;
                    border: 1px solid #c0c0c0;
                    border-radius: 3px;
                    padding: 5px 15px;
                    font-weight: bold;
                }
                QPushButton:hover {
                    background-color: #d0d0d0;
                }
                QPushButton:pressed {
                    background-color: #c0c0c0;
                }
            """)
        else:  # dark theme
            self.setStyleSheet("""
                QDialog {
                    background-color: #2b2b2b;
                    color: #ffffff;
                }
                QTextEdit {
                    background-color: #1e1e1e;
                    color: #ffffff;
                    border: 1px solid #555555;
                    border-radius: 3px;
                    font-family: Consolas, 'Courier New', monospace;
                    font-size: 10pt;
                }
                QPushButton {
                    background-color: #404040;
                    color: #ffffff;
                    border: 1px solid #555555;
                    border-radius: 3px;
                    padding: 5px 15px;
                    font-weight: bold;
                }
                QPushButton:hover {
                    background-color: #505050;
                }
                QPushButton:pressed {
                    background-color: #606060;
                }
            """)

    def generateReport(self):
        """生成连接检查报告"""
        nodes = self.audio_canvas.getAllNodes()
        connections = self.audio_canvas.getAllConnections()

        report = "连接检查报告:\n\n"

        # 检查未连接的输入端口
        unconnected_inputs = []
        for node in nodes:
            # 使用属性检查确保是有效的节点
            if hasattr(node, 'inputPorts'):
                for port_name, port in node.inputPorts.items():
                    if not port.connections:
                        node_name = node.nodeData.get('name', 'Unknown') if hasattr(node, 'nodeData') else 'Unknown'
                        unconnected_inputs.append(f"{node_name}.{port_name}")

        report += f"未连接的输入端口 ({len(unconnected_inputs)}):\n"
        if unconnected_inputs:
            for port in unconnected_inputs:
                report += f"  - {port}\n"
        else:
            report += "  无\n"

        report += "\n"

        # 检查未连接的输出端口
        unconnected_outputs = []
        for node in nodes:
            # 使用属性检查确保是有效的节点
            if hasattr(node, 'outputPorts'):
                for port_name, port in node.outputPorts.items():
                    if not port.connections:
                        node_name = node.nodeData.get('name', 'Unknown') if hasattr(node, 'nodeData') else 'Unknown'
                        unconnected_outputs.append(f"{node_name}.{port_name}")

        report += f"未连接的输出端口 ({len(unconnected_outputs)}):\n"
        if unconnected_outputs:
            for port in unconnected_outputs:
                report += f"  - {port}\n"
        else:
            report += "  无\n"

        report += f"\n总连接数: {len(connections)}"

        self.text_edit.setText(report)